/*
 * Licensed by the Bunie Developmentgroup
 */
package handler;

import Protocol.ColumnNamePacket;
import Protocol.PacketFactory;
import Protocol.PacketTypeKeeper;
import Protocol.QueryRequestPacket;
import Protocol.QueryResponsePacket;
import config.Content;
import gui.BarGraphPanel;
import gui.PieChartPanel;
import gui.TablePanel;
import gui.QueryButton;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JScrollPane;

/**
 * ActionListener for QueryButtons.
 * Controls all management information panels.
 * @author Joep Kemperman
 */
public class PanelController implements ActionListener {

	private TablePanel tablePanel;
	private BarGraphPanel barGraphPanel;
	private PieChartPanel pieChartPanel;
	private JScrollPane tableScrollPane;

	/**
	 * Constructor. Adds all panels for the object to control.
	 * @param tbp Table panel
	 * @param tsp Table scroll pane
	 * @param bgp Bargraph panel
	 * @param pcp Pie chart panel
	 */
	public PanelController(TablePanel tbp, JScrollPane tsp,
			BarGraphPanel bgp, PieChartPanel pcp) {
		tablePanel = tbp;
		barGraphPanel = bgp;
		pieChartPanel = pcp;
		tableScrollPane = tsp;
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		String query = ((QueryButton) e.getSource()).getQuery();
		ArrayList<String[]> managementInfo = getManagementInfo(query);
		setPanels(managementInfo);
		setTableHeader(query);
	}

	/**
	 * Sends query and receives management information.
	 * @return Table with management information.
	 */
	private ArrayList<String[]> getManagementInfo(String query) {
		ArrayList<String[]> info;
		QueryRequestPacket request =
				(QueryRequestPacket) PacketFactory.getPacket(PacketTypeKeeper.QUERYREQUESTPACKET);
		request.setQuery(query);
		QueryResponsePacket response = null;
		try {
			response =
					(QueryResponsePacket) Content.connection.communicate(request);
		} catch (Exception ex) {
			Content.log.println("Failed to get management info");
		}
		return response.getListWithAccounts();
	}

	/**
	 * Tells panels to display management information, if they are able to.
	 * @param managementInfo Management information.
	 */
	private void setPanels(ArrayList<String[]> managementInfo) {
		setTable(managementInfo);
		//Not supported yet. Needs working piechartpanel/graphbarpanel
		if (barGraphPanel.isGraph(managementInfo))
			setGraphBar(managementInfo);
		else
			barGraphPanel.setInformationNotSuitable();
		if (pieChartPanel.isGraph(managementInfo))
			setPieChart(managementInfo);
		else
			pieChartPanel.setInformationNotSuitable();
	}
	
	/**
	 * Creates table based on management information.
	 */
	private void setTable(ArrayList<String[]> managementInfo) {
		tableScrollPane.invalidate();
		tablePanel.setManagementInfo(managementInfo);
		tableScrollPane.validate();
	}

	/**
	 * Creates bar graph based on management information.
	 */
	private void setGraphBar(ArrayList<String[]> managementInfo) {
		barGraphPanel.setManagementInfo(managementInfo);
	}

	/**
	 * Creates pie chart based on management information.
	 */
	private void setPieChart(ArrayList<String[]> managementInfo) {
		pieChartPanel.setManagementInfo(managementInfo);
	}

	/**
	 * Gets the column names and sends them to the table panel
	 * @param query Query to get column names from
	 */
	private void setTableHeader(String query) {
		QueryRequestPacket request = 
				(QueryRequestPacket) PacketFactory.getPacket(PacketTypeKeeper.QUERYREQUESTPACKET);
		request.setStatus(QueryRequestPacket.COLUMN);
		request.setQuery(query);
		ColumnNamePacket columnNamePacket = null;
		try {
			columnNamePacket = (ColumnNamePacket) Content.connection.communicate(request);
		} catch (Exception ex) {
			Content.log.println("Couldn't get column names");
		}
		tablePanel.setColumnNames(columnNamePacket.getColumnNames());
	}
}
